package com.etsmtl.log320.sudo;

import java.util.StringTokenizer;

public class Grid {
	
	boolean hasBeenInitiated = false;
	
	//private Tile[][] gameTiles;
	private int[][] gameTiles;
	
	private boolean[][] isOnColumn = new boolean[9][9];
	private boolean[][] isOnRow = new boolean[9][9];
	private boolean[][] isOnRegion = new boolean[9][9];
	/**
	 * 
	 * @param gridValue		a String of 81 numerical characters comprised between 0 and 9
	 */
	public Grid(String gridValue){
		assert gridValue.length()==81;
		//gameTiles = new Tile[9][9];
		gameTiles = new int[9][9];
		//fill the grid with the string values
		char[] gridValueArray = gridValue.toCharArray();
		//StringTokenizer stk = new StringTokenizer(gridValue,"");

		int i =0;
		int j = 0;
		int k = 0;
		int value;
		try{
			for (i=0;i<9;i++){
				for(j=0;j<9;j++){
					value = Integer.parseInt(Character.toString(gridValueArray[k]));
					gameTiles[i][j] = value;
					k++;
				}
			}
		}
		catch(Exception e){
		}
		
		for(int y = 0; y<9; y++){
			for(int x = 0; x<9; x++){
				isOnColumn[y][x] = isOnRow[y][x] = isOnRegion[y][x] = false;
			}
		}
		
		//place to test individually the validates
		//boolean test = validateRow(1,0); //true
		//test = validateRow(3,0); //false
		//test = validateColumn(1,1); //false
		//test = validateColumn(6,1); //true
		//test = validateRegion(5,4,3); //false
		//test = validateRegion(1,4,3); //true
	}
	
	/*
	public boolean isTileModifiable(int x, int y){
		return gameTiles[y][x].isModifiable();		
	}
	*/
	
	public String getGridContent(){
		StringBuilder sb = new StringBuilder();
		int x,y;
		for(y = 0; y < 9; y++){
			for(x = 0;x<9;x++){
				sb.append(gameTiles[y][x]);
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
	public int getTileValue(int x, int y){
		return gameTiles[y][x];		
	}

	public void setTileValue(int value, int x, int y){
		gameTiles[y][x] = value;
		isOnColumn[x][value-1] = isOnRow[y][value-1] = isOnRegion[3*(y/3)+(x/3)][value-1] = true;
	}
	
	public void resetTile(int valueToReset, int x, int y){
		gameTiles[y][x] = 0;
		isOnColumn[x][valueToReset-1] = isOnRow[y][valueToReset-1] = isOnRegion[3*(y/3)+(x/3)][valueToReset-1] = false;
	}
	
	public boolean validate(int value, int x, int y){
		return (!isOnColumn[x][value-1] && !isOnRow[y][value-1] && !isOnRegion[3*(y/3)+(x/3)][value-1]);
		//return (validateRow(value,y) && validateColumn(value,x) && validateRegion(value,x,y));
	}
	
	public void buildValidationTable(){
		for(int y = 0; y<9; y++){
			for(int x = 0; x<9; x++){
				int value = gameTiles[y][x];
				if (value != 0)
					isOnColumn[x][value-1] = isOnRow[y][value-1] = isOnRegion[3*(y/3)+(x/3)][value-1] = true;
			}
		}
		int i = 0;
	}
	/*	
	private boolean validateRow(int value, int y){
		int i=0;
		for(i=0;i<9;i++){
			if(gameTiles[y][i].GetValue() == value){
				return false;
			}
		}
		return true;
	}
	
	private boolean validateColumn(int value, int x){
		assert(x<9);
		int j=0;
		for(j=0;j<9;j++){
			if(gameTiles[j][x].GetValue() == value){
				return false;
			}
		}
		return true;
	}
	
	private boolean validateRegion(int value, int x, int y){
		int regionStartX = x - (x%3);
		int regionStartY = y - (y%3);

		int i,j;
		for(i=regionStartX;i<=regionStartX+2;i++){
			for(j=regionStartY;j<=regionStartY+2;j++){
				if(gameTiles[j][i].GetValue() == value){
					return false;
				}
			}
		}
		return true;
	}
	*/
}
